package sw.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sw.dao.GroupDao;
import sw.dao.ProjectDao;
import sw.dao.UserDao;
import sw.dao.UserGroupDao;
import sw.entity.Group;
import sw.entity.Project;
import sw.entity.User;
import sw.entity.UserGroup;
import sw.model.MUserGroup;
import sw.service.IGroupAuthManger;

@Service
public class GroupAuthMangerImpl implements IGroupAuthManger {
	@Autowired
	private UserGroupDao userGroupDao;
	
	@Autowired
	private GroupDao groupDao;
	
	@Autowired
	private UserDao userDao;
	@Autowired
	private ProjectDao proDao;

	@Override
	public List<MUserGroup> findAllGroups(Integer projectId) {
		List<UserGroup> userGroupList =  userGroupDao.findByProjectId(projectId);
		Map<Integer,MUserGroup> userGroupMap = new HashMap<Integer, MUserGroup>();
		
		for(UserGroup userGroup : userGroupList){
			Integer groupId = userGroup.getGroup().getId();
			MUserGroup model;
			if(userGroupMap.keySet().contains(groupId)){
				 model = userGroupMap.get(groupId);
			}else{
				 model = new MUserGroup();
			}
			if(userGroup.getIsHeadman() == 1){//组长
				model.setHeadMan(userGroup.getUser().getName());
			}else{//组员
				if(model.getUserNames()==null){
					model.setUserNames(userGroup.getUser().getName()); 
				}else{ 
					model.setUserNames(model.getUserNames()+","+userGroup.getUser().getName());
				}	
			}
			model.setGroupName(userGroup.getGroup().getName());//小组名称
			userGroupMap.put(groupId, model);
		}
		
		List<MUserGroup> modelList = new ArrayList<MUserGroup>();
		for(Integer key : userGroupMap.keySet()){
			modelList.add(userGroupMap.get(key));
		}
		return modelList;
	}

	@Override
	public Integer addUserFromGroup(MUserGroup groups) {
		if(null == groups) return -1;
		
		Integer projectId = groups.getProjectId();
		Project pro = proDao.findOne(projectId);
		if(pro == null ) return -1;
		
		String groupName = groups.getGroupName();
		List<Group> groupList = groupDao.findByName(groupName);
		if(null == groupList) return -1;
		Group group = groupList.get(0);
		
		Integer headId = groups.getHeadManId();
		if(null != headId ){//保存组长
			User headMan = userDao.findOne(headId);
			if(null != headMan){
				UserGroup userGroup = new UserGroup();
				userGroup.setGroup(group);
				userGroup.setUser(headMan);
				userGroup.setIsHeadman(1);
				userGroup.setProject(pro);
				userGroupDao.save(userGroup);
			}
		}
		
		String strIds = groups.getUserIds();
		if(null != strIds &&! "".equals(strIds)  ){//保存组员
			String[] strGroupArr = strIds.split(",");
			for (int i = 0; i < strGroupArr.length; i++) {
				Integer userId = Integer.valueOf(strGroupArr[i]);
				User user = userDao.findOne(headId);
				if(null == user ) continue;
				UserGroup userGroup = new UserGroup();
				userGroup.setGroup(group);
				userGroup.setUser(user);
				userGroup.setIsHeadman(0);
				userGroup.setProject(pro);
				userGroupDao.save(userGroup);
				
			}
		}
		return 0;
	}

	@Override
	public boolean deleteUserFromGroup(Integer userId, Integer groupId) {
		// TODO Auto-generated method stub
		return false;
	}

}
